package com.jiangman.weather.callback;


import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 */

public class CustomCallback<T> extends Callback<T> {
    RsponseStateListenerImp mResonseStateListenerImp;
    Class<?> mClass;
    boolean isRefresh = false;

    public CustomCallback(RsponseStateListenerImp mResonseStateListener, Class clazz, boolean isRefresh) {
        this.mResonseStateListenerImp = mResonseStateListener;
        this.mClass = clazz;
        this.isRefresh = isRefresh;
    }


    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {

        String string = response.body().string();
        System.out.println("响应："+string);
        T bean = (T) new Gson().fromJson(string, mClass);
        int s = 1;
        return bean;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        mResonseStateListenerImp.error(e);
        System.out.println(e.toString());
    }

    @Override
    public void onResponse(T response, int id) {
        try {
            int status = response.getClass().getDeclaredField("code").getInt(response);
            if (status == 200) {

                mResonseStateListenerImp.success(response, isRefresh);
                String fsdfa="fsdf";
            } else if (status == 404) {
                mResonseStateListenerImp.fail(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void inProgress(float progress, long total, int id) {
        super.inProgress(progress, total, id);
    }





}
